import requests

def download_music(url, local_filename):
    # 发送GET请求
    with requests.get(url, stream=True) as r:
        # 确认请求成功
        r.raise_for_status()
        # 将内容写入文件
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)
    return local_filename

# 音乐文件的URL
# music_url = "https://music.163.com/song/media/outer/url?id=1804320463.mp3"
# 保存到本地的文件名
# local_filename = "d:/项目/python/爬虫/网页/踏山河.mp3"

# 调用函数下载音乐并保存
# download_music(music_url, local_filename)

# print(f"歌曲已下载并保存为 {local_filename}")